//
//  EmotionCollectionView.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/29.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class EmotionCollectionView: UICollectionView
{

    
    //重写CollectionView的init方法，并在这上面重新设置view的布局
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)
    {
        //利用流水布局重新设置collectionView
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        //重新布局界面
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //重新布局界面
    func setUpUI()
    {
        backgroundColor = UIColor.whiteColor()
        
        
        //取出当前的collectionView的layout(flowLayout)
        let layout:UICollectionViewFlowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置flowLayout
        layout.itemSize = CGSize(width: SCREENW, height: 216 - 37)
         // 设置垂直和水平间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        //设置滚动方向
        layout.scrollDirection = .Horizontal
        
        //设置数据源代理
        self.dataSource = self
        
        //注册cell
        self.registerClass(EmotionCollectionViewCell.self, forCellWithReuseIdentifier: "EmotionCell")
        
    }
}
//在分类中实现数据源代理方法
extension EmotionCollectionView:UICollectionViewDataSource
{
    
    //(1)组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
    {
        return EmotionDataTool.sharedTool.AllEmotions.count
    }
    //(2)item
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return EmotionDataTool.sharedTool.AllEmotions[section].count
    }
    //(3)item上的cell内容
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
    
        let cell:EmotionCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("EmotionCell", forIndexPath: indexPath) as! EmotionCollectionViewCell
        
        //设置indexPath
        //cell.indexPath = indexPath
        //设置cell上的model数组
        cell.EmotionModelArray = EmotionDataTool.sharedTool.AllEmotions[indexPath.section][indexPath.item]
        
        return cell
    }
    
}